﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using SocialKit.LightRest;
using SocialKit.LightRest.OAuth;
using SocialKit.SinaWeibo.Api.Extensions;

namespace SocialKit.SinaWeibo.Api
{
    /// <summary>
    /// 表示新浪微博 API 服务调用客户端。
    /// </summary>
    public partial class SinaWeiboClient
    {
        /// <summary>
        /// 表示新浪微博 API 服务的基地址( http://api.t.sina.com.cn/ )。
        /// </summary>
        public const string API_HOST = "http://api.t.sina.com.cn/";

        /// <summary>
        /// 获取注册应用的 AppKey (ConsumerKey)。
        /// </summary>
        public string AppKey { get; private set; }

        /// <summary>
        /// 获取注册应用的 AppSecret (ConsumerSecret)。
        /// </summary>
        public string AppSecret { get; private set; }

        #region OAuth Consumer and REST Client

        Consumer oauthConsumer;

        /// <summary>
        /// 获取新浪微博的 OAuth 信息。
        /// </summary>
        public Consumer OAuthConsumer
        {
            get
            {
                if (oauthConsumer == null)
                {
                    oauthConsumer = new Consumer
                    {
                        Key = this.AppKey,
                        Secret = this.AppSecret,
                        RequestTokenUri = API_HOST + "oauth/request_token",
                        AuthorizeUri = API_HOST + "oauth/authorize",
                        AccessTokenUri = API_HOST + "oauth/access_token",
                    };

                }

                return oauthConsumer;
            }
        }

        RestClient restClient;

        protected RestClient RestApiClient
        {
            get
            {
                if (restClient == null)
                {
                    restClient = new RestClient(API_HOST);
                    restClient.SendAsyncCompleted += OnRestClientSendAsyncCompleted;
                }

                return restClient;
            }
        }

        void OnRestClientSendAsyncCompleted(object sender, SendAsyncCompletedEventArgs e)
        {
        }

        #endregion


        /// <summary>
        /// 创建一个新浪微博 API 服务调用客户端的实例。
        /// </summary>
        /// <param name="appKey">注册应用的 AppKey (ConsumerKey)。</param>
        /// <param name="appSecret">注册应用的 AppSecret (ConsumerSecret)。</param>
        public SinaWeiboClient(string appKey, string appSecret)
        {
            if (string.IsNullOrEmpty(appKey))
                throw new ArgumentNullException("appKey");

            if (string.IsNullOrEmpty(appSecret))
                throw new ArgumentNullException("appSecret");

            this.AppKey = appKey;
            this.AppSecret = appSecret;
        }
    }
}